%include "I:\workdata\706727\Build\Directories_And_SampleRestrictions.sas";
%include "I:\workdata\706727\Build\Programs.sas";
libname dstform "\\srvfsenas1\data\Formater\SAS formater i Danmarks Statistik\SAS_datasaet\Sundhed";
libname exp "I:\Workdata\706727\Build\Data";

data base (drop=befupdSourceYear);
set raw.befupdv (keep= pnr befupdSourceYear koen kom IE_TYPE FOED_DAG where=(befupdSourceyear>1994 and befupdSourceYear<2020 and befupdSourceyear-year(FOED_DAG)<=60 and befupdSourceyear-year(FOED_DAG)>=0));
year=BEFupdSourceYear;
length year 8;
age=year-year(foed_dag);
run;

%macro monthdata; 
  %do t=1 %to 12;
	  data base&t;
	  set base;
	  month=&t;
	  run;
%end;
%mend monthdata;
%monthdata;

data basedatayearmonth;
set %listsets(base,1,12);
run;

proc datasets;
delete base1 base2 base3 base4 base5 base6 base7 base8 base9 base10 base11 base12;
quit;

/********************************************************************************************************************************/
/****** Lists of relevant diagnoses and drugs as identified in CollectData_HealthCareUse_OddsRatio_IdentifyRelevantDiseases *****/
/********************************************************************************************************************************/

%let hospdiag=DJ69 DE87 DK72 DK71 DK70 DJ96 DJ22 DE64 DK59 DB10 DE86 DK86;
%let hospcont=DZ75 DZ51 DZ56 DZ73 DZ91 DZ74 DZ65;
%let hospsymp=DR41 DR40 DR68 DR11 DR52 DR56;
%let hosppois=DT43 DT42 DT50 DT48 DT40 DT96 DT65 DT39 DT46 DT36 DT51 DT45;

%let medlist=A06 A03 M03 A12 A02;



/******************************************************/
/***************** Create dataset *********************/
/******************************************************/

/* Hospital */
%macro wordcount(list);
%local count;
%let count=0;
%do %while(%qscan(&list.,&count.+1,%str( )) ne %str() );
 %let count=%eval(&count.+1);
%end;
&count.
%mend wordcount;

%macro listdiags(diaglist,var);
%do i=1 %to %wordcount(&diaglist.);
%let diag=%scan(&diaglist.,&i.);
	%if &i<%wordcount(&diaglist.) %then
	&var.="&diag." or;
	%else &var.="&diag.";
%end;
%mend;

data pop(drop=LPRadmSourceYear);
set raw.LPRadmv (keep=pnr recnum LPRadmSourceYear d_inddto d_hendto c_adiag c_pattype c_spec c_indm c_henm v_sengdage
where=(LPRadmSourceYear>1994 and c_spec ne "50" and c_spec ne "52"));
year=year(d_inddto);
month=month(d_inddto);
diag4=substr(c_adiag,1,4);
run;

data hospdiags;
set pop;
hospdiag=%listdiags(&hospdiag,diag4);
hospcont=%listdiags(&hospcont,diag4);
hospsymp=%listdiags(&hospsymp,diag4);
hosppois=%listdiags(&hosppois,diag4);
diag2=substr(c_adiag,2,2);
diag1=substr(c_adiag,2,1);

if diag1="E" then diagg=100+substr(c_adiag,3,2)*1;
if diag1="J" then diagg=200+substr(c_adiag,3,2)*1;
if diag1="K" then diagg=300+substr(c_adiag,3,2)*1;
if diag1="I" then diagg=400+substr(c_adiag,3,2)*1;
if diag1="C" then diagg=500+substr(c_adiag,3,2)*1;
if diag1="F" then diagg=600+substr(c_adiag,3,2)*1;
if diag1="X" or  diag1="S" or  diag1="R" or  diag1="T" then diagg=700+substr(c_adiag,3,2)*1;
if diag1="M" then diagg=800+substr(c_adiag,3,2)*1;
if diag1="N" then diagg=900+substr(c_adiag,3,2)*1;
if diag1="O" then diagg=1000+substr(c_adiag,3,2)*1;
if diag1="Z" then diagg=1100+substr(c_adiag,3,2)*1;
if diagg=. then diagg=1200;
if (c_pattype="2" and (d_hendto=d_inddto or d_hendto=.) and (c_indm="1" or c_henm="0")) then c_pattype="3"; /* Fix that ER is not recorded after 2013 */

if c_pattype="0" or c_pattype="1" then inpat=1;
if c_pattype="2" then outpat=1;
if c_pattype="3" then ER=1;
if c_pattype="0" or c_pattype="1" then d_inpat=diagg;
if c_pattype="2" then d_outpat=diagg;
if c_pattype="3" then d_ER=diagg;
if c_pattype="0" or c_pattype="1" then n_inpat=v_sengdage;
if c_pattype="2" then n_outpat=v_sengdage;
if c_pattype="3" then n_ER=v_sengdage;
run;

proc sql;
create table hospdiagsprice as select * from hospdiags as a

left join exp.avhelprice1(keep=c_pattype diag1 mhelprice1 _FREQ_ rename=(_FREQ_=numhel1))  as b on  a.c_pattype=b.c_pattype and a.diag1=b.diag1
left join exp.avhelprice2(keep=c_pattype diag2 mhelprice2 _FREQ_ rename=(_FREQ_=numhel2))  as c on  a.c_pattype=c.c_pattype and a.diag2=c.diag2

left join exp.avambprice1(keep=c_pattype diag1 mambprice1 _FREQ_ rename=(_FREQ_=numamb1))  as d on  a.c_pattype=d.c_pattype and a.diag1=d.diag1
left join exp.avambprice2(keep=c_pattype diag2 mambprice2 _FREQ_ rename=(_FREQ_=numamb2))  as e on  a.c_pattype=e.c_pattype and a.diag2=e.diag2;
quit;

data hospdiagsprice;
set hospdiagsprice;
if (c_pattype="0" or c_pattype="1") and numhel2>=5 then p_inpat=mhelprice2;
if (c_pattype="0" or c_pattype="1") and numhel2<5 then p_inpat=mhelprice1;
if (c_pattype="2") and numamb2>=5 then p_outpat=mambprice2;
if (c_pattype="2") and numamb2<5 then p_outpat=mambprice1;
if (c_pattype="3") and numamb2>=5 then p_er=mambprice2;
if (c_pattype="3") and numamb2<5 then p_er=mambprice1;
run;

proc sql;
create table fhospdiags(where=(pnr ne "")) as select distinct pnr, year, month,
max(hospdiag) as hospdiag, max(hospcont) as hospcont,
max(hospsymp) as hospsymp, max(hosppois) as hosppois,
min(d_inpat) as d_inpat, min(d_outpat) as d_outpat,min(d_ER) as d_ER,
sum(p_inpat) as p_inpat, sum(p_outpat) as p_outpat,sum(p_ER) as p_ER,
sum(inpat) as inpat, sum(outpat) as outpat,sum(ER) as ER,
sum(n_inpat) as n_inpat, sum(n_outpat) as n_outpat,sum(n_ER) as n_ER
from hospdiagsprice
group by pnr, year, month
order by pnr, year, month;
quit;

* Ambulatory visits *;

data somaamb;
set raw.lprbesupdv(keep=d_ambdto recnum);
year=year(d_ambdto);
month=month(d_ambdto);
outpat_v=1;
run;

proc sql;
create table somaambcol as select sum(outpat_v) as outpat_v, recnum, month, year from somaamb
group by recnum,year, month;
quit;

proc sql;
create table somaambcolmatch(where=(pnr ne "")) as select sum(outpat_v) as outpat_v, pnr, month, year from somaambcol as a
left join pop(keep=pnr recnum) as b on a.recnum=b.recnum
group by pnr,year, month;
quit;

/* Merge */
proc sql;
create table fsomahosp as select * from fhospdiags as a 

left join somaambcolmatch as b on a.pnr=b.pnr and a.month=b.month and a.year=b.year;
quit;

/* Medicine */

%macro meddata; 
  %do t=1995 %to 2019;
	data med&t(keep=pnr year month medphyc p_medphyc d_medphyc);
	set lmdb.lmdb&t.(where=(%listdiags(&medlist.,substr(ATC,1,3))));
	year=year(eksd);
	month=month(eksd);
	medphyc=1;
    if ibgp~=. then price=ibgp-ptp;
    if ibgp=. then price=0;
	p_medphyc=price;
	if substr(ATC,1,3)="A02" then d_medphyc=1;
	if substr(ATC,1,3)="A03" then d_medphyc=2;
	if substr(ATC,1,3)="A06" then d_medphyc=3;
	if substr(ATC,1,3)="A12" then d_medphyc=4;
	if substr(ATC,1,3)="M03" then d_medphyc=5;
	run;
	
	proc sql;
	create table medcol as select distinct pnr,year,month,
    sum(medphyc) as medphyc,sum(p_medphyc) as p_medphyc,
    max(d_medphyc) as d_medphyc from med&t 
	group by pnr,year,month
	order by pnr,year,month;
	quit;

	data med&t;
	set medcol;
	run;

%end;
%mend meddata;
%meddata;

data fmeddata(where=(pnr ne ""));
set %listsets(med,1995,2019);
run;

/* Primary care */
data SYSI;
set raw.SYSIupdv (keep= pnr speciale afrper ydtyp ydlant bruhon where=(
(substr(afrper,1,2)>"94" or substr(afrper,1,2)<"05")));
yr=substr(afrper,1,2)*1;
if yr<20 then year=yr+2000;
if yr>20 then year=yr+1900;
length year 8;
month=substr(afrper,3,2)*1;
cont=1;
run;

/* 2005-2015 */;
data SSSY (  where=(year<2020));
set raw.SSSYupd01v (keep= pnr  speciale afrper ydtyp ydlant bruhon);
year=substr(afrper,1,2)*1+2000;
length year 8;
month=substr(afrper,3,2)*1;
run;

data sssy;
set sssy sysi;
run;

data primdata;
set sssy;
spec2=substr(speciale,1,2);
if (%listdiags(01 04 06 07 08 09 10 11 15
16 17 18 19 20 21 23 28 53 54 55 61 64,spec2)) then specdoc=ydlant;
if (%listdiags(01 04 06 07 08 09 10 11 15
16 17 18 19 20 21 23 28 53 54 55 61 64,spec2)) then p_specdoc=bruhon;
if speciale="800101" then docvisit=ydlant;
if speciale="800101" then p_docvisit=bruhon;
if (ydtyp="93" or ydtyp="94") then psyc=ydlant;
if (ydtyp="93" or ydtyp="94") then p_psyc=bruhon;
run;

proc sql;
create table fprimdata(where=(pnr ne "")) as select distinct pnr, year, month,
max(specdoc) as specdoc, max(p_specdoc) as p_specdoc,
sum(docvisit) as docvisit,sum(p_docvisit) as p_docvisit,
sum(psyc) as psyc,sum(p_psyc) as p_psyc
from primdata
group by pnr, year, month
order by pnr, year, month;
quit;

/** Merge final data sets **/

proc sql;
create table finalphysical as select * from basedatayearmonth as a

left join fmeddata as b on a.pnr=b.pnr and a.year=b.year and a.month=b.month
left join fprimdata as c on a.pnr=c.pnr and a.year=c.year and a.month=c.month
left join fsomahosp as d on a.pnr=d.pnr and a.year=d.year and a.month=d.month;
quit;

data finalphysical;
set finalphysical(where=(pnr ne "            "));
run;

proc export data=finalphysical
outfile = "J:\Workdata\706727\Temp\physicalhealth.dta"
replace;
run;

